package com.example.myqcloud.oss.controller;

import com.example.myqcloud.oss.cloud.AbstractCloudStorageService;
import com.example.myqcloud.oss.cloud.CloudStorageProperties;
import com.example.myqcloud.oss.cloud.QcloudCloudStorageService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * 文件上传
 */
@RestController
public class SysOssController {
    @Autowired
    private CloudStorageProperties cloudStorageProperties;
    @Value("${app.iosKey}")
    private String iosKey;
    @Value("${app.androidKey}")
    private String androidKey;
    @Value("${app.path}")
    private String appPath;

    @PostMapping("upload")
    public Map<String, Object> upload(@RequestParam("file") MultipartFile file) throws Exception {
        Map<String, Object> result = new HashMap<>();
        if (file.isEmpty()) {
            result.put("msg", "文件为空");
            return result;
        }

        //上传文件
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        AbstractCloudStorageService storageService = new QcloudCloudStorageService();
        // 指定文件路径
        String url = storageService.upload(file.getBytes(), appPath + "/" + "XMBG" + "." + extension);
        // 默认文件路径(会重命名文件名)
        String url1 = storageService.uploadSuffix(file.getBytes(), extension);

        result.put("url", url);
        result.put("url1", url1);
        return result;
    }

    @GetMapping("/copy")
    public String copy() {
        String iosUrl = "https://codevoid-1257260966.cos.ap-nanjing.myqcloud.com/public/test/app/edc50345283e495fbfbfa6b8d6c6bd98.ipa";
        String iosSourceKey = iosUrl.replace(cloudStorageProperties.getDomain(), "");
        AbstractCloudStorageService storageService = new QcloudCloudStorageService();
        storageService.copy(iosSourceKey, iosKey);

        String androidUrl = "https://codevoid-1257260966.cos.ap-nanjing.myqcloud.com/public/test/app/f43d9d6fb9f84b54be78ff186e4c40ec.apk";
        String androidSourceKey = androidUrl.replace(cloudStorageProperties.getDomain(), "");
        storageService.copy(androidSourceKey, androidKey);
        return "ok";
    }

    @GetMapping("/delete")
    public String delete() {
        String url = "https://codevoid-1257260966.cos.ap-nanjing.myqcloud.com/public/test/app/4cc9614c5ceb470293741c8a9abfa84d.ipa";
        String sourceKey = url.replace(cloudStorageProperties.getDomain(), "");
        AbstractCloudStorageService storageService = new QcloudCloudStorageService();
        storageService.delete(cloudStorageProperties.getBucketName(), sourceKey);
        return "ok";
    }


}